import { defineStore } from 'pinia'
import { ref } from 'vue'

//存储当前用户信息的pinia仓库
export const UserInfoStore = defineStore('userStore', () => {

    // 指定用户信息在本地存储的令牌名称
    const userToken = 'lt_user_token'
    // 指定拦截器中请求头的名称
    const userJwtToken = 'lt_token'


    // 用户信息
    const userInfo = ref({
        id: null,
        username: null,
        email: null,
        phone: null,

        avatarUrl: null,
        bio: null,
        gender: null,
        birthday: null,
        location: null,
        followersCount: null,
        followingCount: null,
        postsCount: null,
        status: null,

        createTime: null,
        updateTime: null
    })




    // 设置用户信息
    const setUserInfo = (data) => {
        userInfo.value = data
    }

    // 获取用户信息
    const getUserInfo = () => {
        return userInfo.value
    }

    // 清除用户信息
    const clearUserInfo = () => {
        Object.keys(userInfo.value).forEach(key => {
            userInfo.value[key] = null;
        });
        localStorage.removeItem(userToken)
    }


    //向外暴露的变量或方法
    return {
        userToken,
        userJwtToken,
        userInfo,
        setUserInfo,
        getUserInfo,
        clearUserInfo
    }
})

